<?php

namespace app\admin\controller\dict;

use app\admin\model\Dict;
use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;

/**
 * @ControllerAnnotation(title="字典数据管理")
 */
class Data extends AdminController
{
    /**
     * 允许修改的字段
     * @var array
     */
    protected $allowModifyFields = [
        'status',
        'sort',
        'remark',
        'title',
        'value',
    ];


    use \app\admin\traits\Curd;
protected $sort=['sort'=>'desc'];
    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new \app\admin\model\DictData();

    }
    /**
     * 下拉选择列表
     * @return \think\response\Json
     */
    public function selectList()
    {
        $fields = input('selectFields');
        $data = $this->model
            ->where('pid',$this->request->param('pid'))
            ->field($fields)
            ->order($this->sort)
            ->select();
        $this->success(null, $data);
    }
    /**
     * @NodeAnotation(title="列表")
     */
    public function index($id=null)
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
            $count = $this->model
                ->where($where)
                ->when($id,function ($query) use ($id){
                    $query->where('pid',$id);
            })
                ->count();
            $list = $this->model
                ->where($where)
                ->when($id,function ($query) use ($id){
                    $query->where('pid',$id);
                })
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        $this->assign('pid', $id);
        return $this->fetch();
    }
    /**
     * @NodeAnotation(title="添加")
     */
    public function add($pid=null)
    {

        if ($this->request->isPost()) {
            $post = $this->request->post();
            $rule = [];
            $this->validate($post, $rule);
            try {
                $save = $this->model->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败:'.$e->getMessage());
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        $this->assign('pid', $pid);
        $this->assign('dict', Dict::find($pid));
        return $this->fetch();
    }
    /**
     * @NodeAnotation(title="编辑")
     */
    public function edit($id)
    {
        $row = $this->model->find($id);
        empty($row) && $this->error('数据不存在');
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $rule = [];
            $this->validate($post, $rule);
            try {
                $save = $row->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        $this->assign('row', $row);
        $this->assign('dict', Dict::find($row['pid']));
        return $this->fetch();
    }

}
